/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true

    },
    {
        id: 'status',
        type: 'int',
        hideQuery:true,
        hideQueryType:'in',
        hideQueryValue:[4,6],
        hide:true

    },
    {
        id: 'id',
        title: '编号',
        type: 'int',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return "<a href='"+ncGlobal.adminRoot+"order/detail?orderId="+record.id+"'>"+value+"</a>";
        }
    },
    {
        id: 'orderNo',
        title: '策略号',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'ag.name',
        title: '代理商',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'u.userName',
        title: '用户名',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'iu.userName',
        title: '投资人',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'name',
        title: '品种',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'direction',
        title: '方向',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable: {
            1: $lang.OrderDirection.T1,
            2: $lang.OrderDirection.T2
        }
    },
    {
        id: 'openWay',
        title: '类型',
        type: 'int',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable: {
            1: $lang.settleWay.T1,
            2: $lang.settleWay.T2
        }
    },
    {
        id: 'follow',
        title: '跟单类型',
        type: 'int',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable: {
            1: $lang.follow.T1,
            2: $lang.follow.T2
        }
    },
    {
        id: 'status',
        title: '状态',
        type: 'int',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable: {
            1: $lang.orderStatus.T1,
            2: $lang.orderStatus.T2,
            3: $lang.orderStatus.T3,
            4: $lang.orderStatus.T4,
            5: $lang.orderStatus.T5,
            6: $lang.orderStatus.T6,
            7: $lang.orderStatus.T7,
            8: $lang.orderStatus.T8,
            9: $lang.orderStatus.T9,
            10: $lang.orderStatus.T10
        }
    },
    {
        id: 'number',
        title: '数量（手）',
        type: 'int',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: false
    },
    {
        id: 'openPrice',
        title: '开仓价',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false
    },
    {
        id: 'settlePrice',
        title: '平仓价',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false
    },
    {
        id: 'synthesisFee',
        title: '交易综合费',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false
    },
    {
        id: 'profit',
        title: '实际盈亏',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (value >= 0) {
                return '<span style="background:red;color: white;">' + value + '</span>';
            } else {
                return '<span style="background:green;color: white;">' + value + '</span>';
            }
        }
    },
    {
        id: 'deposit',
        title: '履约保证金',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'profitDeposit',
        title: '止盈保证金',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'profitStopPrice',
        title: '止盈',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'loseStopPrice',
        title: '止损',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'settleType',
        title: '平仓类型',
        type: 'int',
        headerClass: 'text-center',
        columnClass: 'text-left ',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable: {
            1: $lang.SettleType.T1,
            2: $lang.SettleType.T2,
            3: $lang.SettleType.T3,
            4: $lang.SettleType.T4,
            5: $lang.SettleType.T5
        },
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'createTime',
        title: '发起时间',
        type: 'date',
        format: 'yyyy-MM-dd hh:mm:ss',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range',
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'openTime',
        title: '开仓时间',
        type: 'date',
        format: 'yyyy-MM-dd hh:mm:ss',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range',
        hideType: 'md|sm|xs|lg',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if(record.openTime== undefined){
                return "--";
            }else{
                return record.openTime;
            }
        }
    },
    {
        id: 'settleTime',
        title: '平仓时间',
        type: 'date',
        format: 'yyyy-MM-dd hh:mm:ss',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range',
        hideType: 'md|sm|xs|lg',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if(record.settleTime== undefined){
                return "--";
            }else{
                return record.settleTime;
            }
        }
    },
    {
        id: 'openEntrNo',
        title: '开仓委托编号',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'settleEntrNo',
        title: '平仓委托编号',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'operation',
        title: '操作',
        type: 'string',
        'export':false,
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var html="",
                balance="";
            // 判断是否有编辑权限
            if ($("#orderBalance") && $("#orderBalance").val() == 1) {
                balance = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-thumbs-up'></i>&nbsp;结算&nbsp;</a>";
            }
            if(record.status == 4){
                html = balance;
            }
            return html;
        }
    }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'order/json/balance',
    exportURL: ncGlobal.adminRoot + 'order/json/export',
    exportFileName: '策略结算',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: 'refresh|faseQuery|export[excel]',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {bigDecimal:["amount",'totalLeft'],int:["id","type","status"],Timestamp: ["createTime"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;
grid.fastQueryParameters['in_status'] = [4,6];
//排序
grid.sortParameter.columnId = ['asc_status','desc_createTime'];
//列表结束

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {
        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['in_status'] = [4,6];
            grid.fastQueryParameters['lk_u.user_name_or_lk_orderNo'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }



    //编辑对话框初始化
    $("#editModal").on("show.bs.modal", function (event) {
        //清除错误信息
        $(".alert-danger").remove();
        $("#editForm").psly().reset();
        //获取接受事件的元素
        var button = $(event.relatedTarget);
        //获取data 参数
        var datano = button.data('no');
        //获取列表框中的原始数据
        var gridData = grid.sortOriginalDatas[datano];
        $editForm = $("#editForm");
        $editForm.find('input[name="id"]').val(gridData.id);
        $editForm.find('input[name="settleAmount"]').val('');
        $editForm.find('input[name="settlePrice"]').val('');
        $editForm.find('input[name="settleBusiNo"]').val('');
        $editForm.find('input[name="settleTime"]').val('');
        //清除错误提示
        $(".alert-danger").remove()
    });

    //外部可调用
    return {
        init: function () {
            _bindEvent();
        }
    }
}();

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.init();
})